package com.yonyou.iuap.jxzy.service;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.iuap.baseservice.intg.service.GenericIntegrateService;
import com.yonyou.iuap.baseservice.intg.support.ServiceFeature;
import com.yonyou.iuap.jxzy.dao.AcceptancePaperMapper;
import com.yonyou.iuap.jxzy.dao.DomesticPaperModifyMapper;
import com.yonyou.iuap.jxzy.entity.AcceptancePaper;
import com.yonyou.uap.ieop.security.exception.BusinessException;

@Service
public class DomesticPaperModifyService extends GenericIntegrateService<AcceptancePaper>{

	@Autowired
	private DomesticPaperModifyMapper modifyMapper;
	
	@Override	
	protected ServiceFeature[] getFeats() {
		return new ServiceFeature[0];
	}

	/**
	 * 查询国内废纸历史数据
	 * @param map
	 * @return 
	 */
	public List<AcceptancePaper> queryDomPaper(Map<String, Object> map) {
		List<AcceptancePaper>  acceppaperlist=this.modifyMapper.queryDomPaper(map);
		return acceppaperlist;
		
	}

	/**
	 * 个人历史数据提交及提交校验
	 * @param acceptancepaper
	 * @throws BusinessException 
	 */
	@Transactional
	public void domPapermodify(AcceptancePaper acceptancepaper) throws BusinessException {
		String pk_material =acceptancepaper.getMaterialVariety();//物料品种
		String qualitygrade =acceptancepaper.getQualityGrade();//质量等级
		String unpackno=acceptancepaper.getUnpackNo();//开包件数
		String heapNo=acceptancepaper.getHeapNo();//堆号
		String checkPoint=acceptancepaper.getCheckPoint();//检验点
		String forkliftNo=acceptancepaper.getForkliftNo();//叉车工工号
		String returnGoods=acceptancepaper.getReturnGoods();//退货
		//根据序列号获取当前序列号的有效的检验数据
		List<AcceptancePaper> validatedatas=this.modifyMapper.queryValidateDatas(acceptancepaper.getSerialNo(),acceptancepaper.getId());
		if(validatedatas!=null && validatedatas.size()>=1){//多条有效数据需要校验
			for(AcceptancePaper acceptan:validatedatas){
				if(pk_material!=null && !pk_material.equals(acceptan.getPk_material())){
					throw new BusinessException("物料品种不一致！");
				}
				if(qualitygrade!=null && !qualitygrade.equals(acceptan.getQualityGrade())){
					throw new BusinessException("质量等级不一致！");
				}
				if(unpackno!=null && !unpackno.equals(acceptan.getUnpackNo())){
					throw new BusinessException("开包件数不一致！");
				}
				if(heapNo!=null && !"".equals(heapNo) && 
						acceptan.getHeapNo()!=null && !"".equals(acceptan.getHeapNo()) 
						&& !heapNo.equals(acceptan.getHeapNo())){
					throw new BusinessException("堆号不一致！");
				}
				if(checkPoint!=null && !checkPoint.equals(acceptan.getCheckPoint())){
					throw new BusinessException("检验点不一致！");
				}
				if(forkliftNo!=null && !"".equals(forkliftNo) && 
						acceptan.getForkliftNo()!=null && !"".equals(acceptan.getForkliftNo()) 
						&& !forkliftNo.equals(acceptan.getForkliftNo())){
					throw new BusinessException("叉车工号不一致！");
				}
				if(returnGoods!=null && !"".equals(returnGoods) &&
						acceptan.getReturnGoods()!=null && !"".equals(acceptan.getReturnGoods()) 
						&& !returnGoods.equals(acceptan.getReturnGoods())){
					throw new BusinessException("退货不一致！");
				}
			}
			//更新修改字段到当前检验数据中
			int result1=this.modifyMapper.updateModifyDatas(acceptancepaper);
//			//更新堆号字段到当前序列号的有效的检验数据中
//			int result2=this.modifyMapper.updateModifyHeapNo(acceptancepaper.getHeapNo(),acceptancepaper.getSerialNo());
			if(result1==0){
				throw new BusinessException("修改检验数据失败!");
			}
		}else if(validatedatas==null || validatedatas.size()==0){//只有自己这一条有效数据不需要校验，可以直接修改提交
			int result=this.modifyMapper.updateModifyDatas(acceptancepaper);
			if(result==0){
				throw new BusinessException("修改检验数据失败!");
			}
		}
	}
}
